import { _decorator, Animation, animation, Color, Component, director, EventTouch, find, instantiate, Label, log, Node, NodePool, Prefab, Sprite, Tween, tween, TweenAction, UI, UIOpacity, UITransform, v3, Vec3 } from 'cc';
import { tongyongTS } from '../../../res/jiaoben/tongyongTS/tongyongTS';
import { weixinapi } from '../tongyongTS/weixinAIP';

const { ccclass, property } = _decorator;

@ccclass('TSjieyafangkuai')
export class TSjieyafangkuai extends Component {
    @property(Node) b背景组: Node = null;
    @property(Node) d打底方块: Node = null;
    @property(Node) d打底组: Node = null;
    @property(Node) f方块组: Node = null;
    @property(Node) c触发: Node = null;
    @property(Node) c触发组: Node = null;
    @property(Node) q全局遮罩: Node = null;
    @property(Node) s锁组: Node = null;
    @property(Node) z子方块组: Node = null;
    @property(Node) z箭头组: Node = null;
    @property(Node) b爆炸动画: Node = null;
    @property(Label) j金币文字: Label = null;
    @property(Node) y预设方块: Node = null;
    @property(Node) y预设钱: Node = null;

    readonly c初始X = -297
    readonly c初始Y = -480
    readonly j间隔距离x = 66
    readonly j间隔距离y = 90
    readonly z总行数 = 11
    readonly z总列数 = 10
    readonly a凹槽临时时间 = 100
    readonly d单次增加钱 = 5
    readonly s生产钱概率 = 1
    readonly d道具初始价格 = 50
    readonly d道具每次提价 = 20
    readonly t通关奖励金币 = 6000


    HC_qian = 0;
    HC_naolizhi = 0
    HC_JYFK_CCYX = true

    j教程步骤 = 0
    d道具价格

    d当前行号
    d当前列号
    d当前数值
    s随机最大数 = 4
    j解锁等级 = 4

    f方块属性 = {
        h行号: -1,
        l列号: -1,
        s数值: 0
    }

    s数据管理 = []
    j节点管理: Node[] = []

    c触发点击次数 = 0

    x选中的节点: Node[] = []
    enemyPool_1: NodePool
    enemyPool_qian: NodePool

    ty: tongyongTS

    z遮罩管理 = []

    ceshi() {
        this.合成解锁程序()
    }
    ceshi2() {

    }

    Rand = (a, b) => {
        return Math.floor(Math.random() * (b - a + 1)) + a
    }

    protected onLoad(): void {

        this.ty = find('通用管理').getComponent(tongyongTS)
        this.j生产预设体程序()
        this.h缓存数据()


        this.d道具价格 = this.d道具初始价格
        find('Canvas/整个节点/按钮组/价格数').getComponent(Label).string = this.d道具价格

    }
    start() {
        if (this.HC_JYFK_CCYX) {
            find('Canvas/整个节点/教学组').active = true
        } else { find('Canvas/整个节点/教学组').active = false }


        //生产触发器
        for (let h = 0; h < this.z总行数; h++) {
            //生产打底图案
            let n = instantiate(this.c触发)
            n.setParent(this.c触发组)
            let y = this.c初始Y + h * this.j间隔距离y
            n.setPosition(0, y)
            n.name = h + ''
            n.on(Node.EventType.TOUCH_START, this.点击执行1, this)
        }

        //生产打底图案,建立表格
        for (let h = 0; h < this.z总行数; h++) {
            for (let l = 0; l < this.z总列数; l++) {
                //生产打底图案
                let n = instantiate(this.d打底方块)
                n.setParent(this.d打底组)
                let x = this.c初始X + l * this.j间隔距离x
                let y = this.c初始Y + h * this.j间隔距离y
                n.setPosition(x, y)

                //建立表格
                this.s数据管理.push(0)
                this.j节点管理.push(null)
            }
        }
        //布置锁组
        this.布置锁组()

        this.f方块属性.h行号 = 3
        this.f方块属性.l列号 = -1
        for (let xh = 0; xh < 5; xh++) {
            this.f方块属性.l列号++

            this.c创造方块(this.f方块属性.h行号, this.f方块属性.l列号, 1)
        }

        this.f方块属性.h行号 = 5
        this.f方块属性.l列号 = -1
        for (let xh = 0; xh < 5; xh++) {
            this.f方块属性.l列号++
            this.c创造方块(this.f方块属性.h行号, this.f方块属性.l列号, 1)
        }



    }

    h缓存数据() {
        //判断用户数据不存在则存储为用户初始数据
        function jilu(lab: string, n) {
            let a = localStorage.getItem(lab)
            if (a == null || a == '') { localStorage.setItem(lab, JSON.stringify(n)); }
        }

        jilu('HC_qian', this.HC_qian)
        jilu('HC_naolizhi', this.HC_naolizhi)
        jilu('HC_JYFK_CCYX', JSON.stringify(this.HC_JYFK_CCYX))

        this.HC_qian = JSON.parse(localStorage.getItem('HC_qian'))
        this.HC_naolizhi = JSON.parse(localStorage.getItem('HC_naolizhi'))
        this.HC_JYFK_CCYX = JSON.parse(localStorage.getItem('HC_JYFK_CCYX'))

        //显示数据
        this.j金币文字.string = this.HC_qian + ''
    }

    x新手教程(e: EventTouch) {

        switch (this.j教程步骤) {
            case 0:
                this.点击执行1(e)
                this.j教程步骤++
                find('Canvas/整个节点/教学组/节点1').setPosition(1600, 0)
                find('Canvas/整个节点/教学组/节点2').setPosition(0, 0)
                break;
            case 1:
                this.点击执行1(e)
                this.j教程步骤++
                find('Canvas/整个节点/教学组/节点2').setPosition(1600, 0)
                find('Canvas/整个节点/教学组/节点3').setPosition(0, 0)
                break;
            case 2:
                this.合成()
                this.j教程步骤++
                find('Canvas/整个节点/教学组/节点3').setPosition(1600, 0)
                find('Canvas/整个节点/教学组/节点4').setPosition(0, 0)
                break;
            case 3:
                this.发牌()
                this.j教程步骤++
                find('Canvas/整个节点/教学组/节点4').setPosition(1600, 0)
                find('Canvas/整个节点/教学组/节点5').setPosition(0, 0)
                break;
            case 4:
                find('Canvas/整个节点/教学组').destroy()
                break;
        }

    }



    点击执行1(e: EventTouch) {
        let h还原 = () => {
            for (let n of this.x选中的节点) {
                if (n) {
                    n.setPosition(this.f返回位置(n))
                }
            }
        }

        if (this.c触发点击次数 == 0) {
            log('asd')
            //确定行号
            let hanghao: number = JSON.parse(e.target.name)
            this.d当前行号 = hanghao

            let shuzhi = 0
            let daihao = hanghao * this.z总列数 + 9 + 1
            for (let xh = 0; xh < this.z总列数; xh++) {//确定代号
                daihao--
                if (this.j节点管理[daihao]) {
                    shuzhi = JSON.parse(this.j节点管理[daihao].name).s数值
                    this.d当前数值 = shuzhi
                    //确定同一行相同的节点
                    this.x选中的节点 = []
                    for (let xh = 0; xh < this.z总列数; xh++) {
                        if (this.j节点管理[daihao] && JSON.parse(this.j节点管理[daihao].name).s数值 == shuzhi) {
                            if (JSON.parse(this.j节点管理[daihao].name).h行号 != hanghao) { break }//停止寻找下一行
                            this.x选中的节点.unshift(this.j节点管理[daihao])
                            daihao--
                        }
                    }

                    for (let n of this.x选中的节点) {
                        n.setSiblingIndex(n.parent.children.length)
                        n.setPosition(n.position.x + 10, n.position.y + 10)
                    }

                    if (this.x选中的节点[0]) { this.c触发点击次数 = 1; this.ty.bofangshenyin(3) }
                    break; //停止寻找确定代号
                }
            }
        } else {
            if (this.c触发点击次数 == 1) {
                this.c触发点击次数 = 0

                let hanghao: number = JSON.parse(e.target.name)
                //判断行号是否同一行
                if (this.d当前行号 != hanghao) {    //如果不同行
                    //判断数值是否相同
                    let shuzhi = 0
                    let daihao = hanghao * this.z总列数 + 9 + 1
                    let m目标列号 = this.z总列数
                    for (let xh = 0; xh < this.z总列数; xh++) {//确定代号
                        daihao--
                        m目标列号--
                        if (this.j节点管理[daihao]) {       //如果目标行有节点
                            shuzhi = JSON.parse(this.j节点管理[daihao].name).s数值
                            if (this.d当前数值 == shuzhi) {    //数值相同 
                                this.scheduleOnce(() => {
                                    this.ty.bofangshenyin(28)
                                }, 0.25)
                                let m目标行号 = hanghao

                                //可以移动的数量
                                let k可移送数量 = xh + 1

                                //移动节点去目标行
                                let daihao = this.x选中的节点.length - k可移送数量
                                for (let xh = 0; xh < this.z总列数; xh++) {   //遍历x选中的节点
                                    daihao++
                                    if (this.x选中的节点[daihao]) {
                                        m目标列号++
                                        if (m目标列号 < this.z总列数) {
                                            this.s数据管理[this.d代号计算(this.x选中的节点[daihao])] = 0
                                            this.j节点管理[this.d代号计算(this.x选中的节点[daihao])] = null

                                            this.b变化名字加赋值管理(this.x选中的节点[daihao], m目标行号, m目标列号, null)
                                            let n = this.x选中的节点[daihao]
                                            this.x选中的节点[daihao] = null
                                            this.h缓动位置(n, 0.3)
                                        } else {
                                            h还原()
                                            break
                                        }
                                    } else {
                                        h还原()
                                    }
                                }

                            } else {      //数值相不同
                                this.ty.bofangshenyin(4)
                                h还原()
                            }
                            break; //停止寻找确定代号
                        } else {
                            if (xh == 9) {  //如果没有有节点改行为空的
                                this.scheduleOnce(() => {
                                    this.ty.bofangshenyin(28)
                                }, 0.25)
                                let m目标行号 = hanghao

                                //可以移动的数量
                                let k可移送数量 = 11
                                m目标列号 = -1

                                //移动节点去目标行
                                let daihao = this.x选中的节点.length - k可移送数量
                                for (let xh = 0; xh < this.z总列数; xh++) {   //遍历x选中的节点
                                    daihao++
                                    if (this.x选中的节点[daihao]) {
                                        m目标列号++
                                        if (m目标列号 < this.z总列数) {
                                            this.s数据管理[this.d代号计算(this.x选中的节点[daihao])] = 0
                                            this.j节点管理[this.d代号计算(this.x选中的节点[daihao])] = null

                                            this.b变化名字加赋值管理(this.x选中的节点[daihao], m目标行号, m目标列号, null)
                                            this.h缓动位置(this.x选中的节点[daihao], 0.3)
                                        } else {
                                            h还原()
                                            break
                                        }
                                    } else {
                                        h还原()
                                    }
                                }

                                break; //停止寻找确定代号
                            }
                        }

                    }

                } else {    //如果同一行
                    h还原()
                    this.ty.bofangshenyin(28)
                }
            }
        }
    }

    合成() {
        let shengyinTJ = false
        for (let hanghao = 0; hanghao < this.z总行数; hanghao++) {
            let panduan = []
            for (let liehaohao = 0; liehaohao < this.z总列数; liehaohao++) {
                panduan.unshift(this.s数据管理[hanghao * this.z总列数 + liehaohao])
            }
            let shuzhi = panduan[0]
            for (let l = 0; l < this.z总列数; l++) {
                if (panduan[l] == 0) { break }
                if (panduan[l] == 'x') { break }
                if (panduan[l] != shuzhi) { break }
                if (l == 9) {    //能合成
                    //设置遮罩
                    this.q全局遮罩.setPosition(0, 0)
                    //开启声音播放条件
                    shengyinTJ = true
                    //合成程序
                    let m目标行号 = hanghao
                    let liehao = 9
                    let daihao = m目标行号 * 10 + liehao + 1
                    let shijian = 10
                    for (let xh = 0; xh < 10; xh++) {
                        daihao--
                        shijian--
                        let n = this.j节点管理[daihao]
                        this.j节点管理[daihao] = null
                        this.s数据管理[daihao] = 0
                        if (xh == 0) {
                            tween(n).to(0.1 * shijian, { position: v3(this.c初始X, n.position.y) }).call(() => {
                                this.x销毁方块(n)
                                let hechengshuzhi = shuzhi + 1
                                this.c创造方块(m目标行号, 0, hechengshuzhi)
                                this.c创造方块(m目标行号, 1, hechengshuzhi)
                                this.q全局遮罩.setPosition(-1600, 0)
                                this.ty.z震动执行()
                                //更新最大数
                                if (hechengshuzhi > this.s随机最大数) {
                                    this.合成解锁程序()
                                }
                            }).start()
                        } else {
                            tween(n).to(0.1 * shijian, { position: v3(this.c初始X, n.position.y) }).call(() => {
                                this.x销毁方块(n)

                            }).start()
                        }

                    }

                }
            }
        }
        if (shengyinTJ) {
            this.ty.bofangshenyin(34)
            this.scheduleOnce(() => {
                this.ty.bofangshenyin(15)
            }, 0.9)
        }
    }

    x销毁方块(n: Node) {
        if (n.children[1]) {
            let nqian = n.children[1]
            let wz = nqian.worldPosition
            nqian.setParent(find('Canvas/整个节点'))
            nqian.setWorldPosition(wz)
            tween(nqian).to(1, { position: v3(-43.797, 625.364) })
                .call(() => {
                    this.HC_qian += this.d单次增加钱
                    this.j金币文字.string = this.HC_qian + ''
                    this.enemyPool_qian.put(nqian)
                })
                .start()
        }
        this.enemyPool_1.put(n)
    }


    发牌() {
        //确定能发出去的总个数
        let zonggeshu = 0
        for (let g of this.s数据管理) { if (g == 0) { zonggeshu++ } }
        //设置发出去的个数（总数的一半，向上取整）
        let fageshu = Math.ceil(zonggeshu / 2)
        //找到能发的行数
        let n能发的行数 = []
        for (let hanghao = 2; hanghao < this.z总行数 + 2; hanghao++) {
            let daihao = hanghao * this.z总列数 + this.z总列数 - 1
            if (this.s数据管理[daihao] == 0) {
                n能发的行数.unshift(hanghao)
            }
        }
        if (n能发的行数.length) {
            this.ty.bofangshenyin(24)

            //每行发2-4个(难度0)
            let f0 = () => {
                log('0')
                n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                let cishu = n能发的行数.length - 1
                if (cishu == 0) { cishu = 1 }
                for (let xh = 0; xh < cishu; xh++) {
                    let hanghao = n能发的行数[xh]
                    let zuida = this.s随机最大数 - 1
                    if (zuida < 4) { zuida = 4 }
                    let shuzhi = this.Rand(zuida-1, zuida)
                    for (let c = 0; c < this.Rand(2, 4); c++) {
                        let liehao = 0
                        for (let l = 0; l < this.z总列数; l++) {
                            let daihao = hanghao * this.z总列数 + l
                            if (this.s数据管理[daihao] == 0) {
                                liehao = l
                                this.f发牌方块(hanghao, liehao, shuzhi)
                                fageshu--
                                if (fageshu <= 0) { break }
                                break
                            }
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }

            }
            //每行发2-4个(难度1)
            let f1 = () => {
                log('1')
                n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                let cishu = n能发的行数.length - 1
                if (cishu == 0) { cishu = 1 }
                for (let xh = 0; xh < cishu; xh++) {
                    let hanghao = n能发的行数[xh]
                    let zuida = this.s随机最大数 - 1
                    if (zuida < 4) { zuida = 4 }
                    let shuzhi = this.Rand(1, zuida)
                    for (let c = 0; c < this.Rand(2, 4); c++) {
                        let liehao = 0
                        for (let l = 0; l < this.z总列数; l++) {
                            let daihao = hanghao * this.z总列数 + l
                            if (this.s数据管理[daihao] == 0) {
                                liehao = l
                                this.f发牌方块(hanghao, liehao, shuzhi)
                                fageshu--
                                if (fageshu <= 0) { break }
                                break
                            }
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }

            }
            //每行发两个(难度2)
            let f2 = () => {
                log('2')
                n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                let cishu = n能发的行数.length - 1
                if (cishu == 0) { cishu = 1 }
                for (let xh = 0; xh < cishu; xh++) {
                    let hanghao = n能发的行数[xh]
                    let zuida = this.s随机最大数 - 1
                    let shuzhi = this.Rand(zuida-3, zuida)
                    for (let c = 0; c < 2; c++) {
                        let liehao = 0
                        for (let l = 0; l < this.z总列数; l++) {
                            let daihao = hanghao * this.z总列数 + l
                            if (this.s数据管理[daihao] == 0) {
                                liehao = l
                                this.f发牌方块(hanghao, liehao, shuzhi)
                                fageshu--
                                if (fageshu <= 0) { break }
                                break
                            }
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }

            }
            //每行发2-4个(难度3)
            let f3 = () => {
                log('3')
                for (let xh = 0; xh < 50; xh++) {
                    n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                    let hanghao = n能发的行数[0]
                    let zuida = this.s随机最大数 - 2
                    let shuzhi = this.Rand(zuida-4, zuida)
                    for (let c = 0; c < this.Rand(2, 4); c++) {
                        let liehao = 0
                        for (let l = 0; l < this.z总列数; l++) {
                            let daihao = hanghao * this.z总列数 + l
                            if (this.s数据管理[daihao] == 0) {
                                liehao = l
                                this.f发牌方块(hanghao, liehao, shuzhi)
                                fageshu--
                                if (fageshu <= 0) { break }
                                break
                            }
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }

            }
            //每行发两个(难度4)
            let f4 = () => {
                log('4')
                for (let xh = 0; xh < 50; xh++) {
                    n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                    let hanghao = n能发的行数[0]
                    let zuida = this.s随机最大数 - 1
                    let shuzhi = this.Rand(zuida-5, zuida)
                    for (let c = 0; c < 2; c++) {
                        let liehao = 0
                        for (let l = 0; l < this.z总列数; l++) {
                            let daihao = hanghao * this.z总列数 + l
                            if (this.s数据管理[daihao] == 0) {
                                liehao = l
                                this.f发牌方块(hanghao, liehao, shuzhi)
                                fageshu--
                                if (fageshu <= 0) { break }
                                break
                            }
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }

            }
            //每行发1个(难度5)
            let f5 = () => {  
                log('5')
                for (let xh = 0; xh < 50; xh++) {
                    n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                    let hanghao = n能发的行数[0]
                    let zuida = this.s随机最大数 - 1
                    let shuzhi = this.Rand(zuida-6, zuida)
                    let liehao = 0
                    for (let l = 0; l < this.z总列数; l++) {
                        let daihao = hanghao * this.z总列数 + l
                        if (this.s数据管理[daihao] == 0) {
                            liehao = l
                            this.f发牌方块(hanghao, liehao, shuzhi)
                            fageshu--
                            if (fageshu <= 0) { break }
                            break
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }
            }
            //每行发1个只发7一下(难度6)
            let f6 = () => {
                log('6')
                for (let xh = 0; xh < 50; xh++) {
                    n能发的行数.sort(() => { return Math.random() > 0.5 ? -1 : 1 }) //打乱数组
                    let hanghao = n能发的行数[0]
                    let shuzhi = this.Rand(1, 7)
                    let liehao = 0
                    for (let l = 0; l < this.z总列数; l++) {
                        let daihao = hanghao * this.z总列数 + l
                        if (this.s数据管理[daihao] == 0) {
                            liehao = l
                            this.f发牌方块(hanghao, liehao, shuzhi)
                            fageshu--
                            if (fageshu <= 0) { break }
                            break
                        }
                    }
                    if (fageshu <= 0) {
                        break
                    }
                }
            }


            let zu = []
            //发牌难度
            switch (this.j解锁等级) {
                case 4:
                    zu = [f1]
                    break;
                case 5:
                    zu = [f0, f1]
                    break;
                case 6:
                    zu = [f0, f1, f2]
                    break;
                case 7:
                    zu = [f0, f1, f2, f3]
                    f1()
                    break;
                case 8:
                    zu = [f0, f1, f2, f3, f4]
                    break;
                case 9:
                    zu = [f0, f1, f2, f3, f4, f5]
                    break;
            }
            this.ty.s随机打乱(zu)

            let jiugeshu = 0
            let nandu = 1
            for (let num of this.s数据管理) {
                if (num == 9) {
                    jiugeshu++
                    if (jiugeshu >= 6) {
                        nandu = 2
                        break
                    }
                }
            }
            if (nandu == 1) {
                zu[0]()
            } else {
                f6()
            }



        }
    }

    c创造方块(行号: number, 列号: number, 数值: number) {
        let enemy: Node = null;
        if (this.enemyPool_1.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            enemy = this.enemyPool_1.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 instantiate 重新赋值
            enemy = instantiate(this.y预设方块);
        }
        enemy.setParent(this.f方块组) // 将生成的敌人加入节点树 
        let shuxing = {
            h行号: 行号,
            l列号: 列号,
            s数值: 数值
        }
        enemy.name = JSON.stringify(shuxing)
        enemy.setPosition(this.f返回位置(enemy))
        enemy.angle = 0
        enemy.scale = v3(1, 1, 1)
        this.b变化造型(enemy)
        this.s数据管理[this.d代号计算(enemy)] = this.q取数值(enemy).s数值
        this.j节点管理[this.d代号计算(enemy)] = enemy
        enemy.setPosition(this.f返回位置(enemy))


    }

    c创造金币(fujiedian: Node) {
        let ysqian: Node = null;
        if (this.enemyPool_qian.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            ysqian = this.enemyPool_qian.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 instantiate 重新赋值
            ysqian = instantiate(this.y预设钱);
        }
        ysqian.setParent(fujiedian) // 将生成的敌人加入节点树 
        ysqian.setPosition(20, 31)
        ysqian.setSiblingIndex(1)
    }

    f发牌方块(行号: number, 列号: number, 数值: number) {
        let enemy: Node = null;
        if (this.enemyPool_1.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            enemy = this.enemyPool_1.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 instantiate 重新赋值
            enemy = instantiate(this.y预设方块);
        }
        enemy.setParent(this.f方块组) // 将生成的敌人加入节点树 
        let shuxing = {
            h行号: 行号,
            l列号: 列号,
            s数值: 数值
        }
        enemy.name = JSON.stringify(shuxing)

        this.b变化造型(enemy)
        this.s数据管理[this.d代号计算(enemy)] = this.q取数值(enemy).s数值
        this.j节点管理[this.d代号计算(enemy)] = enemy
        enemy.setPosition(0, -700)
        enemy.angle = 0
        enemy.scale = v3(1, 1, 1)
        tween(enemy).to(0.3, { position: this.f返回位置(enemy) }).start()

        let a = this.Rand(1, 10)
        if (a <= this.s生产钱概率) {
            this.c创造金币(enemy)
        }
    }

    q取数值(n: Node): { h行号: -1, l列号: -1, s数值: 0 } {
        let a = JSON.parse(n.name)
        return a
    }

    h缓动位置(n: Node, shijian) {
        this.q全局遮罩.setPosition(0, 0)
        this.z遮罩管理.push(1)
        tween(n).to(shijian, { position: this.f返回位置(n) }).call(() => {
            this.z遮罩管理.pop()
            if (!this.z遮罩管理[0]) {
                this.q全局遮罩.setPosition(-1600, 0)
            }
        }).start()
    }

    f返回位置(n: Node): Vec3 {
        let fksx = JSON.parse(n.name)
        let x = this.c初始X + fksx.l列号 * this.j间隔距离x
        let y = this.c初始Y + fksx.h行号 * this.j间隔距离y
        let v = v3(x, y)
        return v
    }

    b变化名字加赋值管理(n: Node, 行号: number, 列号: number, 数值: number) {
        this.f方块属性.h行号 = this.q取数值(n).h行号
        this.f方块属性.l列号 = this.q取数值(n).l列号
        this.f方块属性.s数值 = this.q取数值(n).s数值

        if (行号 != null) { this.f方块属性.h行号 = 行号 }
        if (列号 != null) { this.f方块属性.l列号 = 列号 }
        if (数值 != null) { this.f方块属性.s数值 = 数值 }
        n.name = JSON.stringify(this.f方块属性)

        this.s数据管理[行号 * 10 + 列号] = this.q取数值(n).s数值
        this.j节点管理[行号 * 10 + 列号] = n
    }

    b变化造型(n: Node) {
        let fksx = JSON.parse(n.name)
        n.children[0].children[0].children.forEach((e) => {
            this.ty.x修改透明度(e, 0)
        })

        switch (fksx.s数值) {
            case 1:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 2:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 3:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 4:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 5:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 6:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 7:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 8:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 9:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
            case 10:
                this.ty.x修改透明度(n.children[0].children[0].children[fksx.s数值 - 1], 255)
                break;
        }
    }

    d代号计算(n: Node): number {
        let fksx = JSON.parse(n.name)
        let daihao = fksx.l列号 + fksx.h行号 * 10
        return daihao
    }

    j生产预设体程序() {
        this.enemyPool_1 = new NodePool();
        let initCount = 5;
        for (let i = 0; i < initCount; ++i) {
            let enemy = instantiate(this.y预设方块);
            this.enemyPool_1.put(enemy);
        }

        this.enemyPool_qian = new NodePool();
        initCount = 5;
        for (let i = 0; i < initCount; ++i) {
            let ysqian = instantiate(this.y预设钱);
            this.enemyPool_qian.put(ysqian);
        }
    }



    布置锁组() {
        let f1 = (n: Node, n2: Node) => {
            this.ty.bofangshenyin(4)
            n.getComponent(UIOpacity).opacity = 0
            tween(n.getComponent(UIOpacity)).to(0.3, { opacity: 255 }).delay(0.5).to(0.6, { opacity: 0 }).start()
            this.ty.d抖动(n2)
        }

        let f = (hanghao1, neirong) => {
            let liehao = 0
            let hanghao = hanghao1
            for (let xh = 0; xh < this.z总列数; xh++) {
                this.s数据管理[liehao + hanghao * this.z总列数] = neirong
                liehao++
            }
        }
        let ns = null
        //布置锁位置
        ns = find('Canvas/整个节点/道具组/锁1')
        ns.setPosition(0, this.c初始Y + 0 * this.j间隔距离y)

        ns = find('Canvas/整个节点/道具组/锁2')
        ns.setPosition(0, this.c初始Y + 1 * this.j间隔距离y)

        ns = this.s锁组.children[0]
        ns.setPosition(0, this.c初始Y + 7 * this.j间隔距离y)
        f(7, 'x')
        //按下程序
        ns.on(Node.EventType.TOUCH_START, (e: EventTouch) => {
            f1(e.target.children[2], e.target.children[1])
        }, this)

        ns = this.s锁组.children[1]
        ns.setPosition(0, this.c初始Y + 8 * this.j间隔距离y)
        f(8, 'x')
        //按下程序
        ns.on(Node.EventType.TOUCH_START, (e: EventTouch) => {
            f1(e.target.children[2], e.target.children[1])
        }, this)


        ns = this.s锁组.children[2]
        ns.setPosition(0, this.c初始Y + 9 * this.j间隔距离y)
        f(9, 'x')
        //按下程序
        ns.on(Node.EventType.TOUCH_START, (e: EventTouch) => {
            f1(e.target.children[2], e.target.children[1])
        }, this)


        ns = this.s锁组.children[3]
        ns.setPosition(0, this.c初始Y + 10 * this.j间隔距离y)
        f(10, 'x')
        //按下程序
        ns.on(Node.EventType.TOUCH_START, (e: EventTouch) => {
            f1(e.target.children[2], e.target.children[1])
        }, this)

        //初始进度显示内容
        //子方块动画



    }

    j进度显示(num) {
        switch (num) {
            case 5:

                this.z子方块组.children[0].scale = v3(1, 1)
                this.z子方块组.children[0].children[1].getComponent(UIOpacity).opacity = 255

                this.z箭头组.children[0].getComponent(UIOpacity).opacity = 255
                break;
            case 6:

                this.z子方块组.children[1].scale = v3(1, 1, 1)
                this.z子方块组.children[1].children[1].getComponent(UIOpacity).opacity = 255

                this.z箭头组.children[1].getComponent(UIOpacity).opacity = 255
                break;
            case 7:
                this.z子方块组.children[2].scale = v3(1, 1, 1)
                this.z子方块组.children[2].children[1].getComponent(UIOpacity).opacity = 255
                this.z箭头组.children[2].getComponent(UIOpacity).opacity = 255
                break;
            case 9:

                this.z子方块组.children[3].scale = v3(1, 1, 1)
                this.z子方块组.children[3].children[1].getComponent(UIOpacity).opacity = 255

                this.z箭头组.children[3].getComponent(UIOpacity).opacity = 255
                break;
            case 10:

                this.z子方块组.children[4].scale = v3(1, 1, 1)

                this.z箭头组.children[3].getComponent(UIOpacity).opacity = 0
                break;
        }
    }

    合成解锁程序() {
        this.s随机最大数++
        this.j解锁等级++

        if (this.j解锁等级 > 4) {
            if (this.HC_JYFK_CCYX) {
                this.HC_JYFK_CCYX = false
                localStorage.setItem('HC_JYFK_CCYX', JSON.stringify(this.HC_JYFK_CCYX))
            }
        }

        if (this.j解锁等级 != 8) { this.ty.bofangshenyin(33) }
        let f = (hanghao1, neirong) => {
            let liehao = 0
            let hanghao = hanghao1
            for (let xh = 0; xh < this.z总列数; xh++) {
                this.s数据管理[liehao + hanghao * this.z总列数] = neirong
                liehao++
            }
        }

        let n = null
        switch (this.j解锁等级) {
            case 5:
                n = this.s锁组.children[0]
                tween(n).to(0.8, { position: v3(-1600, n.position.y) }).start()
                this.j进度显示(this.j解锁等级)
                f(7, 0)
                    ; break
            case 6:
                n = this.s锁组.children[1]
                tween(n).to(0.8, { position: v3(-1600, n.position.y) }).start()
                this.j进度显示(this.j解锁等级)
                f(8, 0)
                    ; break
            case 7:
                n = this.s锁组.children[2]
                tween(n).to(0.8, { position: v3(-1600, n.position.y) }).start()
                this.j进度显示(this.j解锁等级)
                f(9, 0)
                    ; break
            case 9:
                n = this.s锁组.children[3]
                tween(n).to(0.8, { position: v3(-1600, n.position.y) }).start()
                this.j进度显示(this.j解锁等级)
                f(10, 0)
                    ; break
            case 10:
                //胜利了
                this.j进度显示(this.j解锁等级)
                this.s胜利执行()
                    ; break
        }

    }

    清屏() {
        if (this.HC_qian >= this.d道具价格) {
            this.ty.z震动执行()
            this.HC_qian -= this.d道具价格
            find('Canvas/整个节点/进度组/显示组/金币显示/金币数').getComponent(Label).string = this.HC_qian + ''
            this.d道具价格 += this.d道具每次提价
            find('Canvas/整个节点/按钮组/价格数').getComponent(Label).string = this.d道具价格

            this.ty.bofangshenyin(11)
            this.b爆炸动画.setPosition(0, 0)
            this.b爆炸动画.getComponent(Animation).play()

            let kong = []
            for (let n in this.s数据管理) {
                if (this.s数据管理[n] != 'x') {
                    this.s数据管理[n] = 0
                }
                if (this.j节点管理[n]) {
                    let fangkuai = this.j节点管理[n]
                    this.j节点管理[n] = null
                    kong.push(0)
                    let t = tween(fangkuai).delay(0.3).to(0.3, { scale: v3(1.2, 1.2, 1.2) }).to(0.3, { scale: v3(0.8, 0.8, 0.8) })
                        .call(() => {
                            this.x销毁方块(fangkuai)
                            kong.pop()
                            if (kong.length == 0) {
                                this.q全行发牌()
                            }
                        })
                    let t2 = tween(fangkuai).delay(0.1).to(0.5, { angle: -360 })
                    tween(fangkuai).parallel(t, t2).start()

                }
            }
        } else {
            this.ty.bofangshenyin(4)
            let n = find('Canvas/整个节点/道具组/对话框').getComponent(UIOpacity)
            n.opacity = 0
            tween(n).to(0.3, { opacity: 255 }).delay(0.6).to(0.3, { opacity: 0 }).start()
        }
    }

    q全行发牌() {
        for (let h = 2; h < this.z总行数; h++) {
            if (this.s数据管理[h * this.z总列数] == 0) {
                if (h - 1 > this.s随机最大数 - 1) { break }
                for (let l = 0; l < this.z总列数; l++) {
                    this.f发牌方块(h, l, h - 1)
                }
            }
        }
    }

    j广告解锁锁1(leixin) {
        if (leixin == 1) {
            let n = find('Canvas/整个节点/道具组/锁1')
            n.setPosition(-1600, 0)
        }
        if (leixin == 2) {
            let n = find('Canvas/整个节点/道具组/锁2')
            n.setPosition(-1600, 0)
        }
    }

    g关闭锁1(leixin) {
        if (leixin == 1) {
            let n = find('Canvas/整个节点/道具组/锁1')
            n.setPosition(0, this.c初始Y + 0 * this.j间隔距离y)
        }
        if (leixin == 2) {
            let n = find('Canvas/整个节点/道具组/锁2')
            n.setPosition(0, this.c初始Y + 1 * this.j间隔距离y)
        }

    }

    s胜利执行() {
        this.ty.c创建banner(2)
        let n = find('Canvas/弹出组/弹出框_胜利')
        n.setPosition(0, 0)
        n.scale = v3(0.1, 0.1, 0.1)
        tween(n).to(0.3, { scale: v3(1, 1, 1) })
            .call(() => {
                this.j奖励动画()
            })
            .start()

    }

    j奖励动画() {
        this.ty.bofangshenyin(14)
        let jcwzn = this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/领取按钮组/jingbi普通/数量文字')
        let dbwzn = this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/领取按钮组/jingbi多倍/数量文字')
        let ydzs = this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/领取按钮组/shaizixiao')
        this.ty.奖励金币b倍率动画(this.t通关奖励金币, jcwzn, dbwzn, ydzs)
        this.ty.b播放粒子(this.ty.find查找('Canvas/弹出组/弹出框_胜利/caidai2'))

    }

    P普通领取() {
        this.ty.bofangshenyin(13)
        this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/领取按钮组').setPosition(4000, 4000)
        this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/返回和图标组').setPosition(0, 0)
        let qq = this.HC_qian + this.t通关奖励金币
        this.HC_qian = qq
        localStorage.setItem('HC_qian', JSON.stringify(qq))
        this.ty.b播放粒子(this.ty.find查找('Canvas/弹出组/弹出框_胜利/掉金币'))

        let qian = this.HC_qian
        let wzqian = find('Canvas/整个节点/进度组/显示组/金币显示/金币数').getComponent(Label)
        wzqian.string = qian + ''
        let duixiang = {
            shuzhi1: qian,
        }
        tween(duixiang)
            .to(3, { shuzhi1: qian + this.t通关奖励金币 }, {
                onUpdate: () => {
                    wzqian.string = duixiang.shuzhi1.toFixed()
                }
            }).start()
    }

    d多倍领取() {
        this.ty.bofangshenyin(13)
        this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/领取按钮组').setPosition(4000, 4000)
        this.ty.find查找('Canvas/弹出组/弹出框_胜利/小组/返回和图标组').setPosition(0, 0)
        let qq = this.HC_qian + (this.t通关奖励金币 * this.ty.b倍率)
        this.HC_qian = qq
        localStorage.setItem('HC_qian', JSON.stringify(qq))
        this.ty.b播放粒子(this.ty.find查找('Canvas/弹出组/弹出框_胜利/掉金币'))

        let qian = this.HC_qian
        let wzqian = find('Canvas/整个节点/进度组/显示组/金币显示/金币数').getComponent(Label)
        wzqian.string = qian + ''
        let duixiang = {
            shuzhi1: qian,
        }
        tween(duixiang)
            .to(3, { shuzhi1: qian + this.t通关奖励金币 }, {
                onUpdate: () => {
                    wzqian.string = duixiang.shuzhi1.toFixed()
                }
            }).start()

        let f = () => {
            console.log('奖励多倍领取')
        }
        this.ty.c创建激励广告(f)     
    }

    
    按钮炫耀一下() {
        weixinapi.f分享()
    }

    //按钮设置
    t弹出设置() {
        this.ty.c创建banner(2)
        this.ty.bofangshenyin(1)
        let n = find('Canvas/弹出组/弹出框_退出')
        this.ty.t弹出通用(n)
    }
    t弹出设置_退出() {
        this.ty.c创建插屏()
        localStorage.setItem('HC_qian', JSON.stringify(this.HC_qian))
        Tween.stopAll()
        director.loadScene('kaishichangjing')
    }

    t弹出设置__取消() {
        this.ty.g关闭banner()
        this.ty.bofangshenyin(1)
        let n = find('Canvas/弹出组/弹出框_退出')
        n.setPosition(-2000, 0)
    }

    t弹出广告提示1(e, eve) {
        this.ty.c创建banner(2)
        this.ty.bofangshenyin(1)
        let n = find('Canvas/弹出组/弹出框_刺激广告1')
        n.setPosition(0, 0)
        n.scale = v3(0.1, 0.1, 0.1)
        tween(n).to(0.3, { scale: v3(1, 1, 1) }).start()
        let bn = this.ty.find查找('Canvas/弹出组/弹出框_刺激广告1/小组/按钮_领取')
        if (eve == 1) {
            this.ty.x修改Button参数(bn, '1')
        }
        if (eve == 2) {
            this.ty.x修改Button参数(bn, '2')
        }
    }
    t弹出广告提示1_取消() {
        this.ty.g关闭banner()
        this.ty.bofangshenyin(1)
        let n = find('Canvas/弹出组/弹出框_刺激广告1')
        n.setPosition(-1600, 0)
    }

    t弹出广告提示1_确定(e, eve) {
        this.ty.g关闭banner()
        let f = () => {
            let n: Node = find('Canvas/弹出组/弹出框_刺激广告1')
            n.setPosition(-1600, 0)

            if (eve == 1) {
                this.j广告解锁锁1(1)
                let nwz = find('Canvas/整个节点/道具组/提示关闭1')
                nwz.getComponent(UIOpacity).opacity = 160

                let shijian = this.a凹槽临时时间
                let wz = shijian + '秒后关闭'
                nwz.getComponent(Label).string = wz
                let t = tween(nwz).delay(1).call(() => {
                    shijian--
                    wz = shijian + '秒后关闭'
                    nwz.getComponent(Label).string = wz
                    if (shijian <= 0) {
                        nwz.getComponent(UIOpacity).opacity = 0
                        this.g关闭锁1(1)
                        Tween.stopAllByTarget(nwz)
                    }
                })
                tween(nwz).repeatForever(t).start()
            }
            if (eve == 2) {
                this.j广告解锁锁1(2)
                let nwz = find('Canvas/整个节点/道具组/提示关闭2')
                nwz.getComponent(UIOpacity).opacity = 160

                let shijian = this.a凹槽临时时间
                let wz = shijian + '秒后关闭'
                nwz.getComponent(Label).string = wz
                let t = tween(nwz).delay(1).call(() => {
                    shijian--
                    wz = shijian + '秒后关闭'
                    nwz.getComponent(Label).string = wz
                    if (shijian <= 0) {
                        nwz.getComponent(UIOpacity).opacity = 0
                        this.g关闭锁1(2)
                        Tween.stopAllByTarget(nwz)
                    }
                })
                tween(nwz).repeatForever(t).start()
            }
        }

        this.ty.c创建激励广告(f)     
    }



}


